package edu.buaa.act.travel.m.page;

import edu.buaa.act.travel.m.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;

public class BaseActivity extends Activity {
	
	protected static final int DIALOG_WAIT = 900;
	protected static final int DIALOG_GET_DATA_FAIL = 901;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	@Override
	public void overridePendingTransition(int enterAnim, int exitAnim) {
		super.overridePendingTransition(R.anim.left_in, R.anim.left_out);
	}
	
	@Override
	protected Dialog onCreateDialog (int id) {
		switch(id) {
		case DIALOG_WAIT:
			final ProgressDialog waitDialog = new ProgressDialog(this);
			waitDialog.setMessage(getString(R.string.message_load_data));
			waitDialog.setCancelable(false);
			
			return waitDialog;
		case DIALOG_GET_DATA_FAIL:
			return new AlertDialog.Builder(this).setTitle(R.string.string_warning)
				.setMessage(R.string.message_get_data_fail)
				.setPositiveButton(android.R.string.ok, null)
				.create();
		}
		return super.onCreateDialog(id);
	}
	
	final protected void setPageTitle(final int resId) {
		setPageTitle(getString(resId));
	}
	
	final protected void setPageTitle(final String title) {
		final TextView textTitle = (TextView) findViewById(R.id.text_title);
		textTitle.setText(title);
	}
	
	final protected void setupRightPanelButton(final int resId, final View.OnClickListener listener) {
		final Button rightPanelButton = (Button) findViewById(R.id.button_right);
		rightPanelButton.setText(getString(resId));
		rightPanelButton.setOnClickListener(listener);
	}
	
	final protected void setupLeftPanelButton() {
		final Button buttonBack = (Button) findViewById(R.id.button_left);
		if(buttonBack != null) {
			buttonBack.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View view) {
					onBackPressed();
				}
			});
		}
	}
	
	final protected void setRightPanelButtonVisibility(final int visibility) {
		final Button buttonRight = (Button) findViewById(R.id.button_right);
		if(buttonRight != null) {
			buttonRight.setVisibility(visibility);
		}
	}
	
	final protected void setLeftPanelButtonVisibility(final int visibility) {
		final Button buttonBack = (Button) findViewById(R.id.button_left);
		if(buttonBack != null) {
			buttonBack.setVisibility(visibility);
		}
	}
	
	final protected void hideKeyboard() {
		if(getCurrentFocus() != null) {
			((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
				.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 
						InputMethodManager.HIDE_NOT_ALWAYS); 
		}
	}
}
